package com.link.common.packet.resp;

import com.link.common.util.BinaryUtils;
import io.netty.buffer.ByteBuf;

import java.util.List;

public record ZonePacket(byte id, String name, List<RegionPacket> regions) {

    public     void  writerTo(  ByteBuf buffer   ){
        buffer.writeByte(id);
        BinaryUtils.writeString(buffer,name);

        buffer.writeInt(regions.size());
        regions.forEach(reg->{
            reg.writeTo(buffer);
        });



    }

    public int length() {

        var regionLength = regions.stream().map(RegionPacket::length).reduce(Integer::sum).orElse(0);
        return   1 + 4 + BinaryUtils.getStringLength(name) + 4 + regionLength;
    }
}
